package com.eduhzy.ttw.teacher.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.StringUtils;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.EventBusTags;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.utils.PatternUtil;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.teacher.R;
import com.eduhzy.ttw.teacher.di.component.DaggerPersonUpdateComponent;
import com.eduhzy.ttw.teacher.mvp.contract.PersonUpdateContract;
import com.eduhzy.ttw.teacher.mvp.presenter.PersonUpdatePresenter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.xw.repo.XEditText;

import org.simple.eventbus.Subscriber;

import butterknife.BindView;
import butterknife.OnClick;

import static com.eduhzy.ttw.commonsdk.core.Constants.MODIFY_PHONE;
import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * @author xubo
 * @date 2018-7-23 9:06:52
 */
@Route(path = RouterHub.APP_UPDATEPHONEACTIVITY)
public class UpdatePhoneActivity extends BaseActivity<PersonUpdatePresenter> implements PersonUpdateContract.View {
    @BindView(R.id.forget_user)
    XEditText forgetUser;
    private String user;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        //如找不到该类,请编译一下项目
        DaggerPersonUpdateComponent
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
        return R.layout.activity_update_phone;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(getActivity(), QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }
    @OnClick(R.id.forget_next)
    public void onViewClicked() {
        KeyboardUtils.hideSoftInput(this);
        user = forgetUser.getText().toString();

        if (StringUtils.isTrimEmpty(user) || !PatternUtil.isMobilePhone(user)) {
            RxUtil.showTipDialog(this, QMUITipDialog.Builder.ICON_TYPE_FAIL, "请输入正确的手机号码!", 1500);
            return;
        }
        ARouter.getInstance()
                .build(RouterHub.APP_PHONEVERIFYACTIVITY)
                .withString(Intent.EXTRA_TITLE, MODIFY_PHONE)
                .withString(Constants.TEMPLATE, Constants.TYPE_REAL_NAME_AUTH)
                .withString(Constants.USERNAME,user)
                .navigation();
    }
    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public Activity getActivity() {
        return this;
    }

     @Subscriber(tag = RouterHub.APP_UPDATEPHONEACTIVITY)
     public void onEvents(Message msg) {
         switch (msg.what) {
             case EventBusTags.GET_CODE_SUCCESS:
                 mPresenter.updateContactByUser(user,(String) msg.obj);
                 break;
                 default:
         }
     }
}
